package com.cxyd.modules.wms.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wms.dto.SupplierDTO;
import com.cxyd.modules.wms.service.SupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 厂家信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;


    @GetMapping("page")
    @RequiresPermissions("wms:supplier:page")
    public Result<PageData<SupplierDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<SupplierDTO> page = supplierService.page(params);

        return new Result<PageData<SupplierDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:supplier:info")
    public Result<SupplierDTO> get(@PathVariable("id") Long id) {
        SupplierDTO data = supplierService.get(id);

        return new Result<SupplierDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:supplier:save")
    public Result save(@RequestBody SupplierDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        supplierService.save(dto);
        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:supplier:update")
    public Result update(@RequestBody SupplierDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        supplierService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:supplier:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        supplierService.delete(ids);

        return new Result();
    }
}